package com.fwj.user.web.rbac;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.user.api.rbac.RBACAuthService;
import com.fwj.user.entity.rbac.auth.Auth;
import com.fwj.user.entity.user.UserBaseInfo;
import com.fwj.user.model.rbac.auth.AuthVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: fwj
 * @Data: 2022/5/1 17:29
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/RBACAuth")
public class RBACAuthController {


    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(RBACAuthController.class);

    @Autowired
    private RBACAuthService authService;

    /**
     * @Description TODO 保存权限，添加或修改
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/saveAuth")
    public JSONObject saveAuth(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            AuthVo authVo = JSONObject.parseObject(jsonObject.toJSONString(),AuthVo.class);
            Auth auth = buildAuth(authVo,userBaseInfo);
            return authService.saveAuth(auth);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    private Auth buildAuth(AuthVo authVo,UserBaseInfo userBaseInfo) {
        Auth auth = new Auth();
        auth.setAuthName(authVo.getAuthName());
        auth.setCreator(userBaseInfo.getId());
        return auth;
    }

    /**
     * @Description TODO 删除权限
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/deleteAuth")
    public JSONObject deleteAuth(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            String authNumber = jsonObject.getString("authNumber");
            return authService.deleteAuth(authNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


    /**
     * @Description TODO 查询角色
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryAuth")
    public JSONObject queryAuth(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            return authService.queryAuth(jsonObject);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    /**
     * @Description TODO 获取角色详情
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/getAuthDetails")
    public JSONObject getAuthDetails(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            String authNumber = jsonObject.getString("authNumber");
            return authService.getAuthDetails(authNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


}
